<template>
  <el-dialog
    v-model="dialogVisible"
    title="登录"
    width="400px"
    :close-on-click-modal="false"
    class="login-dialog"
  >
    <el-form
      ref="loginFormRef"
      :model="loginForm"
      :rules="loginRules"
      label-position="top"
      class="login-form"
    >
      <el-form-item prop="username" label="用户名">
        <el-input
          v-model="loginForm.username"
          placeholder="请输入用户名"
          :prefix-icon="User"
        />
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input
          v-model="loginForm.password"
          type="password"
          placeholder="请输入密码"
          show-password
          :prefix-icon="Lock"
        />
      </el-form-item>
      <div class="form-actions">
        <el-button type="primary" @click="handleLogin" :loading="loading" class="login-button">
          登录
        </el-button>
      </div>
      <div class="form-footer">
        <router-link to="/register" class="register-link">还没有账号？立即注册</router-link>
      </div>
    </el-form>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'

const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true
  },
  redirectPath: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['update:modelValue', 'login-success'])

const router = useRouter()
const userStore = useUserStore()
const loginFormRef = ref(null)
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const dialogVisible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        await userStore.login({
          username: loginForm.username,
          password: loginForm.password
        })
        ElMessage.success('登录成功')
        dialogVisible.value = false
        emit('login-success')
        
        // 如果有重定向路径，则跳转
        if (props.redirectPath) {
          router.push(props.redirectPath)
        }
      } catch (error) {
        ElMessage.error(error.message || '登录失败')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login-dialog {
  :deep(.el-dialog__body) {
    padding: 20px 30px;
  }
}

.login-form {
  .el-form-item {
    margin-bottom: 20px;
  }
}

.form-actions {
  margin-top: 30px;
  text-align: center;
}

.login-button {
  width: 100%;
  padding: 12px 20px;
  font-size: 16px;
}

.form-footer {
  margin-top: 20px;
  text-align: center;
}

.register-link {
  color: #409EFF;
  text-decoration: none;
  font-size: 14px;
}

.register-link:hover {
  color: #66b1ff;
}
</style> 